package com.sort;

import java.util.Arrays;

/**
 * @Package: com.sort
 * @ClassName: Main
 * @Author: h2cloud
 * @CreateTime: 2021/11/3 12:42 上午
 * @Description:
 */


public class Main {
    public static void main(String[] args) {

        int[] arr = new int[20];
        for (int i = 0; i < 20; i++) {
            arr[i] = (int) (Math.random() * 1000);
        }
        System.out.println(Arrays.toString(arr));
        test1(arr);
        quickSort(arr, 0, arr.length - 1);
        System.out.println();
        System.out.println(Arrays.toString(arr));
        // boolean flag = true;
        // for (int i = 0; i < arr.length - 1; i++) {
        //     if (arr[i] > arr[i + 1]) {
        //         flag = false;
        //         break;
        //     }
        // }
        // if (flag) {
        //     System.out.println("true");
        // } else {
        //     System.out.println("false");
        // }
    }

    // 冒泡
    public static void test1(int[] arr) {
        // for (int i = 0; i < arr.length; i++) {
        //     for (int j = 0; j < arr.length - 1 - i; j++) {
        //         if (arr[j] > arr[j + 1]) {
        //             int temp = arr[j];
        //             arr[j] = arr[j + 1];
        //             arr[j + 1] = temp;
        //         }
        //     }
        // }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    public static void test2(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minIndex] > arr[j]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                int temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = temp;
            }
        }
    }

    public static void test3(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int preIndex = i;
            int curVal = arr[i + 1];
            while (preIndex >= 0 && arr[preIndex] > curVal) {
                arr[preIndex + 1] = arr[preIndex];
                preIndex--;
            }
            arr[preIndex + 1] = curVal;
        }
    }

    public static void test4(int[] arr) {
        int len = arr.length;
        int gap = len / 2;
        while (gap > 0) {
            for (int i = gap; i < len; i++) {
                int preIndex = i - gap;
                int curVal = arr[i];
                while (preIndex >= 0 && arr[preIndex] > curVal) {
                    arr[preIndex + gap] = arr[preIndex];
                    preIndex -= gap;
                }
                arr[preIndex + gap] = curVal;
            }
            gap /= 2;
        }
    }

    public static void quickSort(int[] arr, int l, int r) {
        if (l < r) {
            int mid = partion(arr, l, r);
            quickSort(arr, l, mid - 1);
            quickSort(arr, mid + 1, r);
        }
    }

    public static int partion(int[] arr, int l, int r) {
        int privot = arr[l];
        while (l < r) {
            while (l < r && arr[r] >= privot) {
                r--;
            }
            arr[l] = arr[r];
            while (l < r && arr[l] <= privot) {
                l++;
            }
            arr[r] = arr[l];
        }
        arr[l] = privot;
        return l;
    }
}
